﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ServiceModel;
using System.ServiceModel.Description;


namespace IBOXChatHost
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void buttonStart_Click(object sender, RoutedEventArgs e)
        {
            buttonStart.IsEnabled = false;
            Uri tcpAddr = new Uri("net.tcp://" + textBoxIP.Text.ToString() + ":" + textBoxPort.Text.ToString() + "/ChatService/");

            Uri httpAddr = new Uri("http://" + textBoxIP.Text.ToString() + ":" + (int.Parse(textBoxPort.Text.ToString()) + 1).ToString() + "/ChatService/");

            Uri[] baseAddress = { tcpAddr, httpAddr };
            ServiceHost host = new ServiceHost(typeof(WCFService.ChatService), baseAddress);

            NetTcpBinding tcpBinding = new NetTcpBinding(SecurityMode.None, true);
            tcpBinding.MaxConnections = 100;

            host.AddServiceEndpoint(typeof(WCFService.IChatService), tcpBinding, "tcp");

            ServiceMetadataBehavior mBehave = new ServiceMetadataBehavior();
            host.Description.Behaviors.Add(mBehave);

            host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "net.tcp://" + textBoxIP.Text.ToString() + ":" + (int.Parse(textBoxPort.Text.ToString()) - 1).ToString() + "/ChatService/mex");

            try
            {
                host.Open();
            }
            catch (Exception exp)
            {
                textBoxPort.Text = exp.Message.ToString();
            }
        }
    }
}
